package com.teng.smartdrawer;

import android.app.Application;
import android.content.Context;
import android.graphics.Typeface;
import android.os.Environment;
import android.view.ViewConfiguration;

import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.WeakMemoryCache;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.utils.L;
import com.teng.smartdrawer.utils.DeviceInfoUtil;
import com.teng.smartdrawer.utils.PreferenceUtils;

import org.xutils.ex.DbException;
import org.xutils.x;

import java.io.File;
import java.lang.reflect.Field;

/**
 * Created by teng on 16/2/19.
 */
public class MyApplication extends Application{

    private static final int DISK_CACHE_SIZE_BYTES = 50 * 1024 * 1024;
    private static final int MEMORY_CACHE_SIZE_BYTES = 2 * 1024 * 1024;

    public static MyApplication instance;
    public static Context xContext;
    public static boolean isLogin;

    private PreferenceUtils preferenceUtils;
    private static String IMAGE_TEMP_DIR;
    private static String CACHE_DIR;

    // SD卡文件根目录
    private static final String SHAKING_BASE_DIR = "SmartDrawer";
    // 缓存目录
    private static final String CACHE_FILE_DIR = "caches";
    // 自定义缓存目录
    private static final String DEFINED_CACHE_FILE_DIR = Environment.getExternalStorageDirectory() + "/smartDrawer/temp";
    // sd卡根目录
    public static String sdcard_base_path;

    @Override
    public void onCreate() {
        super.onCreate();
        try {
            ViewConfiguration config = ViewConfiguration.get(this);
            Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
            if (menuKeyField != null) {
                menuKeyField.setAccessible(true);
                menuKeyField.setBoolean(config, false);
            }
        }
        catch (Exception ex) {
            // Ignore
        }
        super.onCreate();

        x.Ext.init(this);

        initBaseDir();
        initImageLoader(this);

        preferenceUtils = PreferenceUtils.getInstance(this);

        isLogin = preferenceUtils.getBoolean(PreferenceUtils.USER_IS_LOGIN , false);

        xContext = getApplicationContext();

    }

    public static MyApplication getInstance() {
        if (instance == null) {
            instance = new MyApplication();
        }
        return instance;
    }

    /**
     * 初始化SD卡
     */
    public void initBaseDir() {
        long availableSDCardSpace = DeviceInfoUtil.getExternalStorageSpace();// 获取SD卡可用空间
        if (availableSDCardSpace != -1L) {// 如果存在SD卡
            sdcard_base_path = Environment.getExternalStorageDirectory() + File.separator + SHAKING_BASE_DIR;
        } else if (DeviceInfoUtil.getInternalStorageSpace() != -1L) {
            sdcard_base_path = getFilesDir().getPath() + File.separator + SHAKING_BASE_DIR;
        } else {// sd卡不存在
            // 没有可写入位置

        }
        // 图片缓存目录
        IMAGE_TEMP_DIR = sdcard_base_path + File.separator + "temp";
        // 照片上传缓存目录
        String photoUploadCacheDire = CACHE_DIR = sdcard_base_path + File.separator + CACHE_FILE_DIR;
        // 初始化根目录
        File basePath = new File(sdcard_base_path);
        if (!basePath.exists()) {
            basePath.mkdir();
        }
        // 初始化照片上传缓存目录
        File cachePath = new File(photoUploadCacheDire);
        if (!cachePath.exists()) {
            cachePath.mkdir();
        }
    }

    /**
     * 初始化ImageLoader
     * @param context
     */
    public void initImageLoader(Context context) {
        File cacheDir = new File(IMAGE_TEMP_DIR);

        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
                .threadPoolSize(4)
                .threadPriority(Thread.NORM_PRIORITY - 2)
                .denyCacheImageMultipleSizesInMemory()
                .diskCacheFileNameGenerator(new Md5FileNameGenerator())
                .memoryCache(new WeakMemoryCache())
                .diskCacheFileCount(100)
                .tasksProcessingOrder(QueueProcessingType.LIFO)
                .diskCache(new UnlimitedDiskCache(cacheDir))
                .build();

        ImageLoader.getInstance().init(config);
//        L.writeLogs(false);
    }

}
